import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sainbainu/common/models/response/other_user_info.dart';
import 'package:sainbainu/common/models/response/user_info.dart';
import 'package:sainbainu/common/values/sizes.dart';
import 'package:sainbainu/common/widgets/widget.dart';

import '../controller.dart';

const double kGridNumHeight = 60;

class HeadContent extends GetView<UserDetailController> {
  final VoidCallback onDoFollowTap;
  final VoidCallback? onFansTap;
  final VoidCallback? onFollowTap;
  final OtherUserInfoResponse userInfo;

  const HeadContent({
    super.key,
    required this.onDoFollowTap,
    required this.userInfo,
    this.onFansTap,
    this.onFollowTap,
  });

  @override
  Widget build(BuildContext context) {
    return _buildLayout();
  }

  Widget _buildLayout() {
    return Stack(
      fit: StackFit.expand,
      children: [
        // 封面
        Positioned(
          left: 0,
          right: 0,
          bottom: kProfileTabbarHeight,
          top: 0,
          child: userInfo.cover == null || userInfo.cover!.isEmpty
              ? Image.asset(
                  'assets/images/cover.jpg',
                  width: Get.width,
                  fit: BoxFit.cover,
                )
              : CustomImage(
                  url: userInfo.cover!,
                  width: Get.width,
                  fit: BoxFit.cover,
                ),
        ),
        // 半透明
        Container(color: Colors.black.withOpacity(0.3)),
        // 上层内容
        Positioned(
          // top: controller.statusBarHeight + kToolbarHeight,
          left: 20,
          right: 20,
          bottom: kProfileTabbarHeight + kGridNumHeight + 4,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // 用户信息
              _buildUserInfo(),
              // 用户介绍
              _buildUserIntro(),
            ],
          ),
        ),
        // 点赞等数量
        _buildGridNumText(),
      ],
    );
  }

  /// 用户信息
  Widget _buildUserInfo() {
    return Row(
      children: [
        _buildUserAvatar(),
        _buildUserNameInfo(),
        if (!controller.state.isMe) _buildEditBtn(),
      ],
    );
  }

  /// 头像
  Widget _buildUserAvatar() {
    return Container(
      width: 74,
      height: 74,
      decoration: BoxDecoration(
        border: Border.all(width: 3, color: Colors.white),
        borderRadius: BorderRadius.circular(74),
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(74),
        child: CustomImage(
          url: userInfo.avatar ?? '',
          fit: BoxFit.cover,
        ),
      ),
    );
  }

  /// 用户昵称角色等
  Widget _buildUserNameInfo() {
    return Expanded(
      child: Padding(
        padding: const EdgeInsets.only(left: 10),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              userInfo.nickName ?? '',
              style: const TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.w600,
                color: Colors.white,
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 2),
              child: Text(
                'SNID：${(userInfo.uniqueNo == null || userInfo.uniqueNo!.isEmpty) ? '未设置' : userInfo.uniqueNo}',
                style: const TextStyle(
                  color: Colors.white,
                  fontSize: 12,
                ),
              ),
            ),
            userInfo.officialFlag == 1
                ? Row(
                    children: [
                      Image.asset(
                        'assets/images/auth.png',
                        width: 14,
                        height: 14,
                      ),
                      Padding(
                        padding: const EdgeInsets.only(left: 2),
                        child: Text(
                          userInfo.officialName ?? '',
                          style: const TextStyle(
                            color: Color(0xffFCFCFC),
                            fontSize: 12,
                          ),
                          strutStyle: const StrutStyle(
                            forceStrutHeight: true,
                          ),
                        ),
                      ),
                    ],
                  )
                : const SizedBox.shrink(),
          ],
        ),
      ),
    );
  }

  /// 关注按钮
  Widget _buildEditBtn() {
    return SizedBox(
      width: 70,
      height: 26,
      child: OutlinedButton(
        onPressed: onDoFollowTap,
        style: OutlinedButton.styleFrom(
          foregroundColor: Colors.white,
          backgroundColor: const Color.fromRGBO(239, 239, 239, 0.23),
          side: const BorderSide(
            color: Color.fromRGBO(255, 255, 255, 0.5),
            width: 1,
          ),
          padding: const EdgeInsets.all(0),
          textStyle: const TextStyle(fontSize: 12),
          enableFeedback: true,
        ),
        child: GetBuilder<UserDetailController>(
          id: 'btn',
          builder: (_) => Text(
            _.state.otherUserInfo.followFlag == 1 ? '已关注'.tr : '关注'.tr,
          ),
        ),
      ),
    );
  }

  /// 介绍
  Widget _buildUserIntro() {
    return Padding(
      padding: const EdgeInsets.only(top: 10),
      child: SizedBox(
        height: 62,
        child: Text(
          userInfo.intro ?? '',
          maxLines: 3,
          overflow: TextOverflow.ellipsis,
          style: const TextStyle(
            color: Color(0xffFCFCFC),
            fontSize: 12,
            fontWeight: FontWeight.w500,
          ),
        ),
      ),
    );
  }

  /// 点赞 收藏布局
  Widget _buildGridNumText() {
    return Positioned(
      left: 0,
      right: 0,
      bottom: kProfileTabbarHeight + 10,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          _buildNumTextItem(
            num: userInfo.fansNum.toDouble(),
            label: '粉丝'.tr,
            onTap: onFansTap,
          ),
          Container(width: 0.5, height: 30, color: Colors.white70),
          _buildNumTextItem(
            num: userInfo.followNum.toDouble(),
            label: '关注'.tr,
            onTap: onFollowTap,
          ),
          Container(width: 0.5, height: 30, color: Colors.white70),
          _buildNumTextItem(
            num: userInfo.receiveNum.toDouble(),
            label: '获赞和收藏'.tr,
          ),
        ],
      ),
    );
  }

  /// 点赞 收藏每一项
  Widget _buildNumTextItem({
    required double num,
    required String label,
    VoidCallback? onTap,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: SizedBox(
        width: 120,
        height: kGridNumHeight,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              '$num',
              style: const TextStyle(
                color: Colors.white,
                fontSize: 16,
                fontWeight: FontWeight.w600,
              ),
            ),
            Text(
              label,
              style: const TextStyle(
                color: Colors.white,
                fontSize: 14,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
